/*
 * Created on 2005-12-08
 */
package cn.com.pxto.commons.httputil;

import javax.servlet.http.HttpServletRequest;

import cn.com.pxto.commons.StringUtils;

/**
 * 
 * @author Gauss Cheung
 *
 */
public class ParamUtil {
	/**
	 * ��HttpServletRequest��ȡ�������ΪparamName��String���͵�ֵ�����ֵΪ�գ�����һ��Ĭ��ֵ
	 * @param request HttpServletRequest���� 
	 * @param paramName String ������
	 * @param defaultValue String Ĭ��ֵ
	 * @return String
	 */
	public static String getParams(
			HttpServletRequest request,
			String paramName,
			String defaultValue){
		String value = defaultValue;
		String temp = request.getParameter(paramName);
		if (temp==null) return value;
		value = temp;
		return value;
	}
	/**
	 * ��HttpServletRequest��ȡ�������ΪparamName��Integer���͵�ֵ�����ֵΪ�գ�����һ��Ĭ��ֵ
	 * @param request HttpServletRequest���� 
	 * @param paramName String ������
	 * @param defaultValue Integer Ĭ��ֵ
	 * @return Integer
	 */
	public static int getIntParams(
			HttpServletRequest request,
			String paramName,
			int defaultValue){
		int value = defaultValue;
		try{
			String parameter = request.getParameter(paramName);
			if(parameter==null) return value;
			value = new Integer(parameter).intValue();
		}
		catch (NumberFormatException e){
			return value;
		}

		return value;
	}
	/**
	 * ��HttpServletRequest��ȡ�������ΪparamName��Integer���͵�ֵ�����ֵΪ�գ�����-1
	 * @param request HttpServletRequest���� 
	 * @param paramName String ������
	 * @return Integer
	 */
	public static int getIntParams(
			HttpServletRequest request,
			String paramName){
		String temp = request.getParameter(paramName);
		if (temp != null){
			try{
				return new Integer(temp).intValue();
			}catch(Exception e){
				return -1;
			}
		}
		else return -1;
	}
	/**
	 * ��HttpServletRequest��ȡ�������ΪparamName��Long���͵�ֵ�����ֵΪ�գ�����һ��Ĭ��ֵ
	 * @param request HttpServletRequest���� 
	 * @param paramName String ������
	 * @param defaultValue Long Ĭ��ֵ
	 * @return Long
	 */
	public static long getLongParams(
			HttpServletRequest request,
			String paramName,
			long defaultValue){
		long value = defaultValue;
		value = new Long(request.getParameter(paramName)).longValue();
		return value;
	}
	/**
	 * ��HttpServletRequest��ȡ�������ΪparamName��Long���͵�ֵ�����ֵΪ�գ�����null
	 * @param request HttpServletRequest����
	 * @param paramName String ������
	 * @return Long
	 */
	public static long getLongParams(
			HttpServletRequest request,
			String paramName){
		String temp = request.getParameter(paramName);
		if (temp != null){
			try{
				return new Long(temp).longValue();
			}catch(Exception e){
				return -1L;
			}
		}
		else return -1L;
	}
	/**
	 * ȡ��ʱ�����ת��Ϊ�����͡��������д���
	 * ��������errorMsg��HttpServletRequest�������null��
	 * @param request
	 * @param attr
	 * @return
	 */
	public static Long getTime(HttpServletRequest request,String attr){
		String st=request.getParameter(attr);
		if(st==null) {
			request.setAttribute("errorMsg","ȱ�ٲ���:"+attr);
			return null;
		}
		Long l = StringUtils.dateString2Long(st);
		if(l==null){
			request.setAttribute("errorMsg",attr+"�������");
		}
		return l;
	}	
}
